﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SYH.Public.Model;
using System.Data;
using SYH.Config;

namespace SYH.Public.DAL
{
    public class Gallery : IGallery
    {
        /// <summary>
        /// 获取指定用户的相册列表信息
        /// </summary>
        /// <param name="user">用户编号</param>
        /// <returns></returns>
        public List<Alums> GetAlums(int userId)
        {
            List<Alums> list = new List<Alums>();
            DataTable dt = Common.SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, "select Name,Id from Public_Album where UserId=" + userId).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                list.Add(new Alums { AlumId = Common.TypeParse.ObjectToInt(dt.Rows[i]["Id"], 0), AlumName = dt.Rows[i]["Name"].ToString() });
            }

            return list;
        }

        /// <summary>
        /// 获取指定用户的相册列表信息
        /// </summary>
        /// <param name="user">用户编号</param>
        /// <returns></returns>
        public DataTable GetAlum(int userId)
        {
            List<Alums> list = new List<Alums>();
            DataTable dt = Common.SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, "select Name,Id from Public_Album where UserId=" + userId).Tables[0];
           
            return dt;
        }

        /// <summary>
        /// 获取相册下的所有图片（分页方式）
        /// </summary>
        /// <param name="alumsId"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="allCount"></param>
        /// <returns></returns>
        public List<Image> GetImages(int alumsId, int pageSize, int pageIndex, out int pageCount, out int recordCount)
        {
            List<Image> list = new List<Image>();

            DataTable dt = Common.PublicSqlData.SimplePager(WebConfig.SqlConnString, "Public_AlbumImg", "Name,Url,Id,Prefix,Extension", pageSize, pageIndex, "Id", true, "and IsDelete=0 and AlbumId=" + alumsId, out pageCount, out recordCount).Tables[0];
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(new Image { Id = Common.TypeParse.ObjectToInt(dt.Rows[i]["Id"], 0), Name = dt.Rows[i]["Name"].ToString(), Url = dt.Rows[i]["Url"].ToString(), Prefix = dt.Rows[i]["Prefix"].ToString(), Extension = dt.Rows[i]["Extension"].ToString() });
                }
                return list;
            }

        }

        /// <summary>
        /// 获取相册下的所有图片（分页方式）
        /// </summary>
        /// <param name="alumsId"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="allCount"></param>
        /// <returns></returns>
        public DataTable  GetAblumnImages(int alumsId, int pageSize, int pageIndex, out int pageCount, out int recordCount)
        {
            List<Image> list = new List<Image>();

            DataTable dt = Common.PublicSqlData.SimplePager(WebConfig.SqlConnString, "Public_AlbumImg", "Name,Url,Id,Prefix,Extension", pageSize, pageIndex, "Id", true, "and IsDelete=0 and AlbumId=" + alumsId, out pageCount, out recordCount).Tables[0];
            return dt;

        }

        /// <summary>
        /// 创建相册
        /// </summary>
        /// <param name="alumsName">相册名字</param>
        /// <param name="userId">用户编号</param>
        /// <returns></returns>
        public int CreateAlums(string alumsName, int userId)
        {
            return 100;
        }

        /// <summary>
        /// 将图片添加到相册中
        /// </summary>
        /// <param name="imageName">图片名称</param>
        /// <param name="alumnId">相册编号</param>
        public int InsertImageToAlums(int alumnId, string imageName, int UserId, string title)
        {
            var extsion = title.Substring(title.LastIndexOf("."));
            var array = imageName.Split('.');
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("insert into Public_AlbumImg ");
            sbSql.Append("(AlbumId,Name,UserId,Extension,Prefix,Title) values ");
            sbSql.Append("(" + alumnId + ",'" + imageName + "'," + UserId + ",'." + extsion + "','" + array[0] + "','" + title + "')");
            sbSql.Append(";select @@IDENTITY");
            int imageId = Convert.ToInt32(Common.SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, sbSql.ToString()));

            string sql = "insert into dbo.Public_AlbumImageDetail(ImageId) values(" + imageId + ")";

            Common.SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, sql);
            return imageId;
        }
    }
}
